package net.bcxuexi.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

import net.bcxuexi.config.Gloab;


public class FileOutMsg extends java.lang.Thread {
	
	File file = new File(Gloab.getInstance().getLogFile());
	
	public FileOutMsg(){
		if(!file.exists()){
			file.getParentFile().mkdirs();
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static Queue<String> queue = new LinkedList<String>();  
	
	public static void setData(String msg){
		synchronized (queue) {
			queue.offer(msg);
		}
	}
	
	@Override
	public void run() {
		try {
			FileOutputStream out=new FileOutputStream(file,true);
			while(true){
				String msg = queue.poll();
				if(msg != null) {
					out.write((msg+"\r\n").getBytes());
				}else{
					sleep(1000*1);
				}
			}
				
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}        
		
		
	}
	
	

}
